package com.ts.api.http.org.biz;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.algorithm.encrypt.symmetric.AES;
import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.base.MapUtils;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.gitee.apanlh.util.net.http.HttpRequestHeader;
import com.gitee.apanlh.util.valid.ValidParam;
import com.ts.api.http.org.entity.apply.OrgApplyHeHuiHua;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.core.utils.ApiFieldConverter;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMd5ConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5Apply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5DataExchange;

import java.util.Map;

/**
 *  合汇花
 *
 *  @author WJB
 */
@ApiPlan(productId = "3", type = ApiPlanConvertType.ORG_PHONE_MD5)
public class ApiOrgPhoneMd5HeHuiHua implements ApiOrgPhoneMd5ConvertHandler {

    private static String PLATFORM_ID = "20250224113692855144";
    private static String KEY = "UzVtmfSZX4IM6yaA3iITpF0eiausyWjq";
    private static String IV = "5fcbc15390019d95";

    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) {
        HttpRequestHeader httpRequestHeader = HttpRequestHeader.create("platformId", PLATFORM_ID);
        apiDataExchangeParameter.setHttpRequestHeader(httpRequestHeader);
        //  车产
        String car = ChooseEq.create()
                .when(apiDataExchangeParameter.getCarInfo(), "1", "0")
                .when(apiDataExchangeParameter.getCarInfo(), "2", "1")
                .when(apiDataExchangeParameter.getCarInfo(), "3", "1")
                .when(apiDataExchangeParameter.getCarInfo(), "4", "1")
                .end("0");
        //  房产
        String house = ChooseEq.create()
                .when(apiDataExchangeParameter.getRealEstateInfo(), "1", "0")
                .when(apiDataExchangeParameter.getRealEstateInfo(), "2", "2")
                .when(apiDataExchangeParameter.getRealEstateInfo(), "3", "2")
                .when(apiDataExchangeParameter.getRealEstateInfo(), "4", "2")
                .when(apiDataExchangeParameter.getRealEstateInfo(), "5", "2")
                .when(apiDataExchangeParameter.getRealEstateInfo(), "6", "2")
                .end("0");
        //  社保
        String sbScop = ChooseEq.create()
                .when(apiDataExchangeParameter.getSocialSecurity(), "1", "0")
                .when(apiDataExchangeParameter.getSocialSecurity(), "2", "4")
                .when(apiDataExchangeParameter.getSocialSecurity(), "3", "4")
                .when(apiDataExchangeParameter.getSocialSecurity(), "4", "4")
                .end("0");
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(car).append(",").append(house).append(",").append(sbScop);

        Map<Object, Object> map = MapUtils.newHashMap(newMap -> {
            newMap.put("city_name", apiDataExchangeParameter.getCity());
            newMap.put("mobile_encry", apiDataExchangeParameter.getPhoneMd5());
            newMap.put("source", stringBuilder);
            newMap.put("item_no", 0);
        });
        return JsonUtils.toJson(map);
    }

    @Override
    public RespOrgPhoneMd5DataExchange respDataExchange(String respJson) {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 0)) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        return RespOrgPhoneMd5DataExchange.ofSuc();
    }

    @Override
    public Object apply(ApiApplyParameter apiApplyParameter) {
        HttpRequestHeader httpRequestHeader = HttpRequestHeader.create("platformId", PLATFORM_ID);
        apiApplyParameter.setHttpRequestHeader(httpRequestHeader);

        OrgApplyHeHuiHua orgApplyHeHuiHua = new OrgApplyHeHuiHua();
        orgApplyHeHuiHua.setCname(apiApplyParameter.getUserName());
        orgApplyHeHuiHua.setMobile(apiApplyParameter.getPhone());
        orgApplyHeHuiHua.setCity(apiApplyParameter.getCity());

        ApiFieldConverter apiFieldConverter = ApiFieldConverter.create(apiApplyParameter);
        //  贷款金额
        String money = ChooseEq.create()
                .when(apiApplyParameter.getLoanAmount(), "1", "3万")
                .when(apiApplyParameter.getLoanAmount(), "2", "5万")
                .when(apiApplyParameter.getLoanAmount(), "3", "10万")
                .when(apiApplyParameter.getLoanAmount(), "4", "20万")
                .end("3万");
        orgApplyHeHuiHua.setMoney(money);
        orgApplyHeHuiHua.setAge(apiFieldConverter.getAgeInt());

        //  性别
        String sex = apiApplyParameter.getSex();
        if (ValidParam.isNotEmpty(sex)) {
            Integer sexChoose = ChooseEq.create()
                .when(sex, "0", 1)
                .when(sex, "1", 2)
            .end();
            orgApplyHeHuiHua.setSex(sexChoose);
        }
        orgApplyHeHuiHua.setSource(3);

        //  车产
        Integer car = ChooseEq.create()
                .when(apiApplyParameter.getCarInfo(), "1", 0)
                .when(apiApplyParameter.getCarInfo(), "2", 1)
                .when(apiApplyParameter.getCarInfo(), "3", 1)
                .when(apiApplyParameter.getCarInfo(), "4", 1)
                .end(0);
        orgApplyHeHuiHua.setCar(car);

        //  房产情况
        Integer house = ChooseEq.create()
                .when(apiApplyParameter.getRealEstateInfo(), "1", 0)
                .when(apiApplyParameter.getRealEstateInfo(), "2", 1)
                .when(apiApplyParameter.getRealEstateInfo(), "3", 1)
                .when(apiApplyParameter.getRealEstateInfo(), "4", 1)
                .when(apiApplyParameter.getRealEstateInfo(), "5", 1)
                .when(apiApplyParameter.getRealEstateInfo(), "6", 1)
                .end(0);
        orgApplyHeHuiHua.setHouse(house);

        //  社保
        orgApplyHeHuiHua.setShebao(apiFieldConverter.hasSocialSecurityToNumber());
        //  保险
        orgApplyHeHuiHua.setBaodan(apiFieldConverter.hasInsurancePolicyToNumber());
        //  公积金
        orgApplyHeHuiHua.setFunds(apiFieldConverter.hasProvidentFundToNumber());
        //  信用卡
        orgApplyHeHuiHua.setCredit(apiFieldConverter.hasCreditLimitToNumber());
        //  芝麻分
        String zhima = ChooseEq.create()
                .when(apiApplyParameter.getSesameCredit(), "1", "600")
                .when(apiApplyParameter.getSesameCredit(), "2", "649")
                .when(apiApplyParameter.getSesameCredit(), "3", "699")
                .when(apiApplyParameter.getSesameCredit(), "4", "700")
                .end();
        orgApplyHeHuiHua.setZm_score(zhima);
        //  打卡工资
        Integer salaryPayMethod = ChooseEq.create()
                .when(apiApplyParameter.getSalaryPayMethod(), "1", 1)
                .when(apiApplyParameter.getSalaryPayMethod(), "2", 0)
                .when(apiApplyParameter.getSalaryPayMethod(), "3", 0)
                .end();
        orgApplyHeHuiHua.setSalary(salaryPayMethod);
        //  营业执照
        orgApplyHeHuiHua.setBusiness_license(apiFieldConverter.hasBizLicenseYearToNumber());
        //  ip
        orgApplyHeHuiHua.setIp(apiApplyParameter.getIp());
        orgApplyHeHuiHua.setItem_no(0);
        AES aes = AES.createCbc(KEY.getBytes(), IV.getBytes());

        String data = aes.encryptToBase64Str(JsonUtils.toJson(orgApplyHeHuiHua));
        Map<Object, Object> map = MapUtils.newHashMap(newMap -> {
            newMap.put("data", data);
        });
        return JsonUtils.toJson(map);
    }

    @Override
    public RespOrgPhoneMd5Apply respApply(String respJson) {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 0)) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        return RespOrgPhoneMd5Apply.ofSuc();
    }
}
